/*
 * townMode.hpp
 *
 *  Created on: Sep 1, 2013
 *      Author: jason
 */

#ifndef TOWNMODE_HPP_
#define TOWNMODE_HPP_

#include "TownEvent.hpp"
//#include "TriggerArea.hpp"
//#include "../ObjectManager.hpp"
#include "../GameState.hpp"
#include "../Dialogue.hpp"
#include "../../InputEngine.hpp"
#include "../../Common.hpp"
#include "../../mapLoader.hpp"
#include "../../Objects/Characters.hpp"
#include "../../luaManager.hpp"

namespace OForce_Mode {

// Current state effects how input should be handled
enum CURRENT_STATE {EXPLORE, DIALOGUE, TREASURE, MENU};

class TriggerArea;
class ObjectManager;

class TownMode : public State {
	friend void BindDefinitionCode();
public:
	TownMode(std::string filename);
	~TownMode();
	// Game Functions
	void characterAction();
	void update();
	void draw();
	void addCharacter(OForce_Object::Character *character);
	void setPlayerCharacter(OForce_Object::Character* character) {playerCharacter = character;}
	void setCamera(OForce_Object::Character* character) {camera = character;}
	void addTreasure(OForce_Object::Treasure *treasure);
	void addTriggerArea(TriggerArea *trig);
	void playTrack(int n);
	void setState(CURRENT_STATE state) {currentState = state;}
	void setCooldown(float t) {coolDown = t;}
	void startFading();
	bool isCurrentlyFading() {return isFading;}
	CURRENT_STATE getCurrentState() {return currentState;}
	ObjectManager *getObjectManager() {return objectManager;}
	DialogueManager *getDialogueManager() {return dialogueManager;}
	EventManager *getEventManager() {return eventManager;}
	static TownMode *getCurrentInstance() {return currentInstance;}
	// TODO collisionCheck would be better inside of the object manager
//	bool collisionCheck(OForce_Object::Character *curChar, int characterIndex);
//	bool collisionCheck(OForce_Object::Character *curChar, float x, float y);
	// Script functions
	LuaScript &getTownScript() {return townScript;}
	std::string getMapName() {return mFilename;}
	void openMapTableSpace(bool global = false) {
		townScript.openTable(mFilename);
	}
	OForce_Object::Character *getPlayerCharacter() {return playerCharacter;}
	sf::Vector2i getWorldSize() {return worldSize;}
private:
	static TownMode *currentInstance;
	void setupObjects(Tmx::Map *map);
	void drawMap();
	void load();
	void drawBasicUI(); // TODO this should be virtual to handle multiple game modes
	enum Direction {Down, Left, Right, Up};
	CURRENT_STATE currentState;
	// Cooldown so that speaking and treasure interactions don't happen immediately after ending
	float coolDown;
	// whether or not we are currently fading
	bool isFading;
	// whether to fade
	bool mFade;
	// timer for fading the screen in and out
	float fadeTimer;
	// Rectangle to be drawn over screen to give illusion of fading
	sf::RectangleShape fadeRect;
	OForce_Input::InputEngine *mInput;
	int currentLayer;
	// The camera is a pointer to the location of a sprite which may or may
	// not be visible but manages the location of the center of the viewport
	// usually the player character
	OForce_Object::Character *camera;
	OForce_Object::Character *playerCharacter;
//	std::vector<sf::ConvexShape> collisionPolys;
//	std::vector<sf::FloatRect > collisionRects;
	ObjectManager *objectManager;
	DialogueManager *dialogueManager;
	EventManager *eventManager;
	// Keep track of treasure chest for ongoing interactions
	OForce_Object::Treasure *currentTreasure;
	sf::Vector2i source;    // position vector for character TODO move this into character
	sf::Vector2i nTiles;    // Current Number of Map Tiles
	sf::Vector2i worldSize; // # of pixels in map
	sf::Vector2f position;  // Character position vector
	sf::Music mMusic;
	// TODO remove stat* to be replaced by CEGUI
	sf::Image statImage;
	sf::Texture statTexture;
	sf::Sprite statSprite;
	sf::Sprite tiles;
	std::string mFilename;
	Tmx::Map *mMap;
	LuaScript townScript;
	luabind::object scriptUpdateFunction;
	std::string scriptFolder;
	std::string musicFolder;
	std::string mapFolder;
	// Vectors and data to be gotten from the Lua Script
	std::vector<std::string> sound_filenames;
	std::vector<std::string> music_filenames;
};

} // namespace
#endif /* TOWNMODE_HPP_ */
